package com.life.xxx.util;

/**
 * version 1.0
 * author chenyy
 * date 2017.01.01
 * desc Page 用来处理分页的基础类
 */
import java.util.ArrayList;
import java.util.List;

public class Page<T> {
	
	protected int pageNo = 1;
	protected int pageSize = 25;

	// 查询总记录数
	protected int totalCount = 0;
	
	// 查询记录
	protected List<T> result = new ArrayList<T>();
	
	//第一页默认为0
	protected int first = 0;
	
	// 总页数
	private int totalPages;
	
	public Page(){
		
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(final int pageNo) {
		this.pageNo = pageNo;
		if ( pageNo < 1 ) {
			this.pageNo = 1;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(final int pageSize) {
		this.pageSize = pageSize;
	}
	
	public int getSize(){
		return getResult().size();
	}

	//得到开始记录
	public int getFirst() {
		if( pageNo <= 1) {
			first = 0;
		} else {
			first = (( pageNo-1 ) * pageSize );
		}
		return first;
	}

	//给开始记录值赋值
	public void setFirst(int first) {
		this.first = first;
	}

	//获取总记录数.
	public int getTotalCount() {
		return totalCount;
	}

	//给总记录赋值
	public void setTotalCount(final int totalCount) {
		this.totalCount = totalCount;
		int t = getTotalPages();
		setTotalPages( t );
	}

	//计算总页数.
	public int getTotalPages() {
		if ( totalCount < 0 ) {
			return 1;
		} else {
			totalPages = (int)( totalCount / pageSize );
			if ( totalCount % pageSize > 0 ) {
				totalPages ++;
			}
			return totalPages;
		}
	}
	
	//给总页数赋值
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	//获取结果集
	public List<T> getResult() {
		return result;
	}

	//给结果集赋值
	public void setResult(final List<T> result) {
		this.result = result;
	}

}